Return to doc.sitecore.com

Valid for Sitecore 5.2, 5.3
How can I call a custom Error Page when an Item requested in the URL does not exist?

Q:

How can I call a custom Error Page when an Item requested in the URL does not exist?

A:

There are at least two ways.

1.  Using Web.config Parameter

There is a special web.config parameter

<!--  ITEM NOT FOUND HANDLER
            Url of page handling 'Item not found' errors
-->
      
<setting name="ItemNotFoundUrl" value="/Errors/MyErrorPage.html" />

You can replace a Sitecore error page with a custom one.

2.  Write your Custom Processor

You can write your own processor for the httpRequestBegin pipeline:   

<pipelines>

   <httpRequestBegin>

     …

     <processor type="Sitecore.Pipelines.HttpRequest.LayoutResolver, Sitecore.Kernel" />

     <processor type="CustomHttpRequestHandler.MyHttpHandler, CustomHttpRequestHandler" />

     <processor type="Sitecore.Pipelines.HttpRequest.ExecuteRequest, Sitecore.Kernel" />

   </httpRequestBegin>

and use the following code for the CustomHttpRequestHandler: 

namespace CustomHttpRequestHandler
{
  
public class MyHttpHandler
  {
      
public void Process(HttpRequestArgs args)
      {
        
if (args.LocalPath.IndexOf("/sitecore/") == -1 && args.FilePath.IndexOf("/sitecore/") == -1 && Context.Item == null )
         {
            WebUtil.Redirect(
"http://localhost/MyCustomErrorPage.html");
            args.Abort();
            
return;
         }
      }
  }
}